package com.shiyu.common.core.domain;

import com.shiyu.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {
    private int code;
    private String message;
    private T data;
    public static <T> R<T> ok(){
        return assembleResult(null,ResultCode.SUCCESS);
    }

    public static  <T> R<T> ok(T data){
        return assembleResult(data,ResultCode.SUCCESS);
    }
    public static <T> R<T> fail(){
        return assembleResult(null,ResultCode.FAILED);
    }
    public static <T> R<T> fail(int code,String message){
        return assembleResult(code,message,null);
    }
    public static <T> R<T> fail(ResultCode resultCode){
        return assembleResult(null,resultCode);
    }
    private static <T> R<T> assembleResult(T data, ResultCode resultCode){
        R<T> r=new R<>();
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMessage());
        r.setData(data);
        return r;
    }
    private static <T> R<T> assembleResult(int code, String message,T data){
        R<T> r=new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

}
